﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
  public static  class KiemKeKho
    {
        public static Entities.KiemKeKho[] Select(Entities.KiemKeKho nv)
        {
            Entities.KiemKeKho[] list = null;
            try
            {
                string sql = Common.Constants.KiemKeKho.SelectKiemKeKho;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.KiemKeKho.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KiemKeKho row = new Entities.KiemKeKho();
                    row.PhieuKiemKeKhoID = int.Parse(dr[Common.Constants.KiemKeKho.PhieuKiemKeKhoID].ToString());
                    row.MaPhieuKiemKe = dr[Common.Constants.KiemKeKho.MaPhieuKiemKe].ToString();
                    row.NgayKiemKe = Convert.ToDateTime(dr[Common.Constants.KiemKeKho.NgayKiemKe].ToString());
                    row.MaKho = dr[Common.Constants.KiemKeKho.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.KiemKeKho.TenKho].ToString();
                    row.GhiChu = dr[Common.Constants.KiemKeKho.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.KiemKeKho.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.KiemKeKho[0];
                }
                else
                {
                    list = new Entities.KiemKeKho[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KiemKeKho)arr[i];
                    }
                }

                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.KiemKeKho[0]; }
            return list;
        }
        public static int InsertUpdate(Entities.KiemKeKho nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KiemKeKho.InsertUpdateKiemKeKho, cn);
                cmd.Parameters.Add(Common.Constants.KiemKeKho.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.PhieuKiemKeKhoID, SqlDbType.Int).Value = nhom.PhieuKiemKeKhoID;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.MaPhieuKiemKe, SqlDbType.VarChar, 50).Value = nhom.MaPhieuKiemKe;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.NgayKiemKe, SqlDbType.DateTime).Value = nhom.NgayKiemKe;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.MaKho, SqlDbType.VarChar, 50).Value = nhom.MaKho;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.TenKho, SqlDbType.NVarChar, 200).Value = nhom.TenKho;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.GhiChu, SqlDbType.NVarChar, 200).Value = nhom.GhiChu;
                cmd.Parameters.Add(Common.Constants.KiemKeKho.Deleted, SqlDbType.Bit).Value = nhom.Deleted;
                rt = cmd.ExecuteNonQuery();
                    
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }
    }
}
